package rootpackege.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.FileSystemResource;
import org.springframework.security.access.SecurityConfig;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
 * 配置webConfig  其作用如同springMVC.xml文件
 * @author BriansPC
 *
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"rootpackege.spring.controller"} )
public class WebConfg extends WebMvcConfigurerAdapter{
	
	//配置视图解析器
	@Bean
	public ViewResolver viewResolver(){
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	    resolver.setPrefix("/WEB-INF/views/");
	    resolver.setSuffix(".jsp");
	    resolver.setExposeContextBeansAsAttributes(true);
	    return resolver;
	}
	/**
	 * 将静态资源的请求转发到servlet容器(比如tomcat)默认的servlet中
	 */
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		// TODO Auto-generated method stub
		configurer.enable();
	}
	/**
	 * 批量上传 multipart解析器  
	 * springmvc的核心控制器中设置
	 * ---控制器参数中 通过MultipartFile 对象获取上传的文件，通过该对象的方法可以获取上传文件名 文件内容等信息
	 * @return
	 * @throws Exception
	 */
	@Bean
	public MultipartResolver multipartResolver() throws Exception{
		/**
		 * StandardServletMultipartResolver只有无参构造
		 * 且其对应的大小限制等只能通过在web.xml中设置的
		 */
//		return new StandardServletMultipartResolver();
		//配置通用的批量文件上传解析器  上传文件的其他参数可以直接通过set方法限制
		CommonsMultipartResolver  commonsMultipartResolver=  new CommonsMultipartResolver();
		commonsMultipartResolver.setUploadTempDir(new FileSystemResource("/fileTemp"));
		//设置最大文件大小
		commonsMultipartResolver.setMaxUploadSize(1024*1024*5);
		commonsMultipartResolver.setMaxInMemorySize(0);//设置文件总大小为0  标识不限制
		return commonsMultipartResolver;
	}
	
}
